package com.cupcorn.lib.display;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.batch.SpriteBatch;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.util.adt.io.in.IInputStreamOpener;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.cupcorn.lib.Utils;
import com.cupcorn.lib.game.GameEngine;
import com.cupcorn.lib.game.GameSprite;
import com.cupcorn.lib.game.element.GameElementSpriteBatch;

public class DisplaySpriteBatch
{
	int									countSprite;
	
	GameEngine							engine;
	GameElementSpriteBatch[]			gameElementSpriteBatch;
	ITexture							textureAtlas;
	ITextureRegion						textureRegion;
	Bitmap								bitmap;
	
	ArrayList<GameElementSpriteBatch>	listBatch;
	boolean								isArray;
	
	public GameSprite[]						sprite;
	
	public DisplaySpriteBatch(final GameEngine engine, String path, GameElementSpriteBatch[] gameElementSpriteBatch)
	{
		countSprite = gameElementSpriteBatch.length;
		
		this.engine = engine;
		this.gameElementSpriteBatch = gameElementSpriteBatch;
		
		load(path);
		
		isArray = false;
	}
	
	public DisplaySpriteBatch(final GameEngine engine, String path, ArrayList<GameElementSpriteBatch> listBatch)
	{
		countSprite = listBatch.size();
		
		this.engine = engine;
		this.listBatch = listBatch;
		
		load(path);
		
		isArray = true;
	}
	
	private void load(final String path)
	{
		try
		{
			bitmap = BitmapFactory.decodeStream(engine.getAssets().open(path));
			
			textureAtlas = new BitmapTexture(engine.getTextureManager(), new IInputStreamOpener()
			{
				public InputStream open() throws IOException
				{
					return engine.getAssets().open(path);
				}
			});
			
			textureAtlas.load();
			textureRegion = TextureRegionFactory.extractFromTexture(textureAtlas);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	public SpriteBatch newSpriteBatch()
	{
		return newSpriteBatch(engine.getEngine().getScene());
	}
	
	public SpriteBatch newSpriteBatch(Scene scene)
	{
		SpriteBatch spriteBatch = spriteBatch();
		scene.attachChild(spriteBatch);
		return spriteBatch;
	}
	
	public SpriteBatch spriteBatch()
	{
		SpriteBatch batch = new SpriteBatch(textureAtlas, countSprite, engine.getVertexBufferObjectManager());
		sprite = new GameSprite[countSprite];
		
		if (isArray)
		{
			for (int i = 0; i < countSprite; i++)
			{
				float width = 0;
				float height = 0;
				
				if (listBatch.get(i).width != 0 && listBatch.get(i).height != 0)
				{
					width = Utils.getRatioWidth(listBatch.get(i).width, listBatch.get(i).ratio);
					height = Utils.getRatioHeight(listBatch.get(i).height, listBatch.get(i).ratio);
				}
				else
				{
					width = Utils.getRatioWidth(bitmap.getWidth(), listBatch.get(i).ratio);
					height = Utils.getRatioHeight(bitmap.getHeight(), listBatch.get(i).ratio);
				}
				float x = Utils.getPosX(listBatch.get(i).x, width, listBatch.get(i).anchor, listBatch.get(i).ratio);
				float y = Utils.getPosY(listBatch.get(i).y, height, listBatch.get(i).anchor, listBatch.get(i).ratio);
				
				sprite[i] = new GameSprite(x, y, width, height, textureRegion, engine.getVertexBufferObjectManager(), listBatch.get(i).ratio);
				batch.draw(sprite[i]);
				
			}
		}
		else
		{
			for (int i = 0; i < countSprite; i++)
			{
				float width = 0;
				float height = 0;
				
				if (gameElementSpriteBatch[i].width != 0 && gameElementSpriteBatch[i].height != 0)
				{
					width = Utils.getRatioWidth(gameElementSpriteBatch[i].width, gameElementSpriteBatch[i].ratio);
					height = Utils.getRatioHeight(gameElementSpriteBatch[i].height, gameElementSpriteBatch[i].ratio);
				}
				else
				{
					width = Utils.getRatioWidth(bitmap.getWidth(), gameElementSpriteBatch[i].ratio);
					height = Utils.getRatioHeight(bitmap.getHeight(), gameElementSpriteBatch[i].ratio);
				}
				float x = Utils.getPosX(gameElementSpriteBatch[i].x, width, gameElementSpriteBatch[i].anchor, gameElementSpriteBatch[i].ratio);
				float y = Utils.getPosY(gameElementSpriteBatch[i].y, height, gameElementSpriteBatch[i].anchor, gameElementSpriteBatch[i].ratio);
				
				sprite[i] = new GameSprite(x, y, width, height, textureRegion, engine.getVertexBufferObjectManager(), listBatch.get(i).ratio);
				batch.draw(sprite[i]);
			}
		}
		batch.submit();
		
		return batch;
	}
}
